/**
 * Created by dale on 2017/10/9.
 */
var useCarList;
$(document).ready(function () {

    useCarList = $('#useCarList');
    $('.xh-datagrid-toolbar').parseToolbar();
    //1.申请列表  2.审批列表  3.审批通过
    var stateType = addressUri.getParam('stateType');
    useCarList.initDataGrid(baseUrl + 'lm/car/getUseCar?stateType='+stateType).setSelectThenEnable(['bt_list_del'],['bt_list_undo','bt_list_Desc']);

    $('#bt_list_add').click(function () {
        $('#listTable tr:first').hide();
        $('#listTable tr:last').show();
        window.frames['useCarForm'].location.href = baseUrl+'lm/car/useCarForm?stateType='+stateType;
    });

    $('#bt_list_usePrivate').click(function () {
        $('#listTable tr:first').hide();
        $('#listTable tr:last').show();
        window.frames['useCarForm'].location.href = baseUrl+'lm/car/usePrivate?stateType='+stateType;
    });


    $('#bt_list_del').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var ids=useCarList.getDataGridSelectedFields('iUCId');
        myWindow.initPopUp({title: '确定要删除么?', text: '这些车辆申请记录将被删除!'},function () {
            $.ajax({
                url:baseUrl+'lm/car/delUseCar',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data,
                        '',
                        'success'
                    );
                    useCarList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseText'],
                        '',
                        'error'
                    );
                }
            });
        });
    });

    $('#bt_list_search').click(function () {
        useCarList.addDataGridParams(getParams());
    });

    $('#bt_list_Desc').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id=useCarList.getDataGridSelectedField('iUCId');
        window.top.xhTop.showModal({
            title:'查看审批意见',
            content:'<iframe src="'+baseUrl+'lm/car/descList/'+id+'" style="height: 350px;border-style: none;padding: 0px;width: 100%;"></iframe>',
            size:'lg'
        });
    });

    $('#bt_list_undo').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id=useCarList.getDataGridSelectedField('iUCId');
        var carId = useCarList.getDataGridSelectedField('iCarId');
        console.log(carId);
        if(carId == null){
            window.top.swal({
                title: '请填写撤销理由',
                input: 'text',
                showCancelButton: true,
                confirmButtonText: '提交',
                cancelButtonText: '取消',
                showLoaderOnConfirm: true
            }).then(function (result) {
                $.ajax({
                    url:baseUrl+'lm/car/undoUseCar/true',
                    data:{id:id,desc:result},
                    type:'POST',
                    success:function (data) {
                        window.top.swal(
                            data,
                            '',
                            'success'
                        );
                        useCarList.dataGridReload();
                    },
                    error:function (data) {
                        window.top.swal(
                            data['responseText'],
                            '',
                            'error'
                        );
                    }
                });
            },function () {

            });
        }else {
            myWindow.initPopUp({title: '确定要撤销么?', text: '这些车辆申请记录将被撤销!'},function () {
                $.ajax({
                    url:baseUrl+'lm/car/undoUseCar',
                    data:{id:id},
                    type:'POST',
                    success:function (data) {
                        window.top.swal(
                            data,
                            '',
                            'success'
                        );
                        useCarList.dataGridReload();
                    },
                    error:function (data) {
                        window.top.swal(
                            data['responseText'],
                            '',
                            'error'
                        );
                    }
                });
            });
        }
    });

    DataGrid.onLoadSuccess = function () {
        useCarList.dataGridClear();
    };

    DataGrid.onDblClickRow=function (rowIndex, rowData) {
        var iUCId = rowData['iUCId'];
        $('#listTable tr:first').hide();
        $('#listTable tr:last').show();
        if(rowData['iCarId']!=null){
            window.frames['useCarForm'].location.href = baseUrl+'lm/car/useCarForm?iUCId='+iUCId+'&stateType='+stateType;
        }else {
            window.frames['useCarForm'].location.href = baseUrl+'lm/car/usePrivate?iUCId='+iUCId+'&stateType='+stateType;
        }
        return false;
    }
});

function getParams() {
    var key=xhExtra.getValue($('#list_search'));
    var carType=xhExtra.getValue($('#list_carType'));
    var carState = xhExtra.getValue($('#list_carState'));
    return {key:key,carType:carType,carState:carState};
}

